package com.liuxinlong.modules.material.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.liuxinlong.modules.common.controller.request.BaseDeleteReq;
import com.liuxinlong.modules.common.controller.response.BaseResp;
import com.liuxinlong.modules.entity.UnitIndicators;
import com.liuxinlong.modules.material.controller.request.QueryUnitIndicatorsReq;
import com.liuxinlong.modules.material.controller.request.UpdateUnitIndicatorsReq;
import com.liuxinlong.modules.material.service.UnitIndicatorsService;

/**
 * 物料单元指标信息控制层
 * 
 * @author liuxinlong@hotwater.com.cn
 * @version 1.0.0
 * @since 2021-01-17
 */
@RestController
@RequestMapping("/material/unit/indicators")
public class UnitIndicatorsController {
	@Autowired
	private UnitIndicatorsService unitIndicatorsService;

	/**
	 * 分页查询物料单元指标信息
	 * 
	 * @param req 请求信息
	 * @return 物料单元指标信息列表
	 */
	@PostMapping("page")
	public BaseResp pageUnitIndicatorsInfo(@RequestBody QueryUnitIndicatorsReq req) {
		Map<String, Object> queryMap = new HashMap<>();
		queryMap.put("startNum", req.getStartNum());
		queryMap.put("pageSize", req.getPageSize());
		queryMap.put("unitId", req.getUnitId());
		queryMap.put("materialId", req.getMaterialId());
		List<Map<String, Object>> dataList = unitIndicatorsService.pageUnitIndicatorsInfo(queryMap);
		int totalNum = unitIndicatorsService.getUnitIndicatorsCount(queryMap);
		BaseResp resp = new BaseResp();
		resp.setRetMessage("分页查询物料单元指标信息成功");
		resp.setDataList(dataList);
		resp.setTotalNum(totalNum);
		return resp;
	}

	/**
	 * 新增物料单元指标信息
	 * 
	 * @param req 请求信息
	 * @return 成功标识
	 */
	@PostMapping("add")
	public BaseResp addUnitIndicatorsInfo(@RequestBody UpdateUnitIndicatorsReq req) {
		UnitIndicators unitIndicators = new UnitIndicators();
		unitIndicators.setUnitId(req.getUnitId());
		unitIndicators.setMaterialId(req.getMaterialId());
		unitIndicators.setRequirement(req.getRequirement());
		unitIndicators.setAttach(req.getAttach());
		unitIndicators.setRemark(req.getRemark());
		unitIndicatorsService.addUnitIndicatorsInfo(unitIndicators);
		BaseResp resp = new BaseResp();
		resp.setRetMessage("新增物料单元指标信息成功");
		return resp;

	}

	/**
	 * 修改物料单元指标信息
	 * 
	 * @param req 请求信息
	 * @return 成功标识
	 */
	@PostMapping("update")
	public BaseResp updateUnitIndicatorsInfo(@RequestBody UpdateUnitIndicatorsReq req) {
		UnitIndicators unitIndicators = new UnitIndicators();
		unitIndicators.setId(req.getId());
		unitIndicators.setUnitId(req.getUnitId());
		unitIndicators.setMaterialId(req.getMaterialId());
		unitIndicators.setRequirement(req.getRequirement());
		unitIndicators.setAttach(req.getAttach());
		unitIndicators.setRemark(req.getRemark());
		unitIndicatorsService.updateUnitIndicatorsInfo(unitIndicators);
		BaseResp resp = new BaseResp();
		resp.setRetMessage("修改物料单元指标信息成功");
		return resp;
	}

	/**
	 * 删除物料单元指标信息
	 * 
	 * @param req 请求信息
	 * @return 成功标识
	 */
	@PostMapping("delete")
	public BaseResp deleteUnitIndicatorsInfo(@RequestBody BaseDeleteReq req) {
		unitIndicatorsService.deleteUnitIndicatorsInfo(req.getId());
		BaseResp resp = new BaseResp();
		resp.setRetMessage("删除物料单元指标信息成功");
		return resp;
	}
}
